package org.zoomdev.zoom.dao.adapters.statement;

import org.zoomdev.zoom.common.caster.ValueCaster;
import org.zoomdev.zoom.dao.adapters.StatementAdapter;

import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * 先转下格式，然后在调用setObject
 * Map/Set等集合形式需要先转成json格式才行
 */
public class CasterStatementAdapter implements StatementAdapter {

    private final ValueCaster valueCaster;


    public CasterStatementAdapter(ValueCaster valueCaster) {
        this.valueCaster = valueCaster;
    }

    @Override
    public void adapt(PreparedStatement statement, int index, Object value) throws SQLException {
        statement.setObject(index, valueCaster.to(value));
    }
}
